import axios, { AxiosError, type AxiosRequestHeaders, type AxiosResponse } from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';
import pinia from '@/stores/index';
import { useUserInfoStore } from '../stores/userInfo';

/* 定义response对象的data接口 */
interface ResponseData<T> {
  code: number;
  data: T;
  message: string;
}

// 配置新建一个 axios 实例
const downLoad = axios.create({
  timeout: 50000,
});

const token = sessionStorage.getItem('token_key')

// 添加请求拦截器
downLoad.interceptors.request.use(
  (config) => {
    // let token = sessionStorage.getItem('Authorization')
    // if (token) {
    // 	(config.headers as AxiosRequestHeaders).token = token
    // }
    (config.headers as AxiosRequestHeaders).Authorization = `${token}`
    return config;
  }
);

// 添加响应拦截器
downLoad.interceptors.response.use(
  /* 约束一下response */
  (response: AxiosResponse<ResponseData<any>>) => {
    try {
      return response
    } catch (error) {
      return Promise.reject('请求数据失败')
    }
  }
);

export default downLoad;
